<?php

require_once(Mage::getBaseDir('lib')."/Excel/PHPExcel.php");
require_once(Mage::getBaseDir('lib')."/Excel/PHPExcel/Writer/Excel2007.php");

class Silk_Zgb_Adminhtml_ConvertController extends Mage_Adminhtml_Controller_Action
{	
	public function indexAction(){
		$this->loadLayout();
		$this->renderLayout();
	}

	public function processingAction()
	{
		//$this->_getSession()->addError("请选择要转换的文件");
        
        //$convert = Mage::getModel('zgb/convert');
        //$items = $convert->processFile("E:\Htdocs\\xdhg\\www\\media\\uploads/f/i/file.xlsx");
        // print_r($items);
        // $fp = fopen('E:\\Htdocs\\xdhg\\documents\\import\\' .'export.csv', 'w');
        // foreach($items as $item)
        // {
        //     fputcsv($fp, $item);
        // }
        // fclose($fp);
        // return;
		//print_r($_FILES);

		$uploader = new Varien_File_Uploader('import_file');
        $uploader->setAllowedExtensions(array('xlsx'));
        $uploader->setAllowRenameFiles(true);
        $uploader->setFilesDispersion(true);
        $path = Mage::getBaseDir('media') . DS . 'uploads' . DS;
        $uploader->save($path, $_FILES[$type]['name'] );
        $filename = $uploader->getUploadedFileName();
        //var_dump($filename);
        $convert = Mage::getModel('zgb/convert');
        $items = $convert->processFile($path.$filename);
        //print_r($items);
        $csvfile = Mage::getBaseDir('media') . DS . 'uploads' . DS .'export.csv';
        @unlink($csvfile);
        $fp = fopen($csvfile, 'w');
        foreach($items as $item)
        {
            fputcsv($fp, $item);
        }
        fclose($fp);

        return $this->_prepareDownloadResponse(
                    'product_import.csv',
                    file_get_contents($csvfile)
                );
		//$this->_redirect('*/*/index');
	}

	
    public function categoryAction()
    {
        //echo Mage::getBaseDir('lib')."/Excel/PHPExcel.php";
        //echo "导出分类数据";

        $objPHPExcel  = new PHPExcel();
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

        $root = Mage::getModel('catalog/category')->load(12);
        $categorys = $root->getChildrenCategories();

        $shidx = 0;
        foreach($categorys as $category)
        {
            if($shidx != 0) $objPHPExcel->createSheet();
            $objPHPExcel->setActiveSheetIndex($shidx);
            $sheet = $objPHPExcel->getActiveSheet();
            $sheet->setTitle($category->getName());
            $sheet->getColumnDimension('B')->setWidth(12);
            $sheet->getColumnDimension('C')->setWidth(50);

            $products = $category->getProductCollection();

            $row = 1;
            foreach($products as $product)
            {
                if($row==1)
                {
                    $sheet->setCellValue("A{$row}","编号");
                    $sheet->setCellValue("B{$row}","SKU");
                    $sheet->setCellValue("C{$row}","名称");
                    $sheet->setCellValue("D{$row}","价格");
                }
                else
                {
                    $product = Mage::getModel('catalog/product')->load($product->getId());
                    $sheet->setCellValue("A{$row}",$product->getId());
                    $sheet->setCellValue("B{$row}",$product->getSku());
                    $sheet->setCellValue("C{$row}",$product->getName());
                    $sheet->setCellValue("D{$row}",$product->getPrice());
                }
                $row++;
            }

            $shidx++;
        }

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename="分类数据.xlsx"');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
    }
}
